package com.jbit.dm212.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.jbit.dm212.common.MD5Util;
import com.jbit.dm212.entity.dto.DmUserDTO;
import com.jbit.dm212.entity.po.DmUser;
import com.jbit.dm212.mapper.DmUserMapper;
import com.jbit.dm212.service.DmUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class DmUserServiceImpl implements DmUserService {
    @Resource
    private DmUserMapper userMapper;
    @Override
    public boolean checkPhone(String phone) {
        MPJLambdaWrapper<DmUser> lambdaWrapper = new MPJLambdaWrapper<>();
        lambdaWrapper.eq(DmUser::getPhone,phone);
        return userMapper.selectCount(lambdaWrapper) == 1;
    }

    @Override
    public boolean register(DmUser user) {
        String md5 = MD5Util.digest(user.getPassword());
        user.setPassword(md5);
        return userMapper.insert(user) == 1;
    }

    @Override
    public DmUserDTO login(DmUser user) {
        MPJLambdaWrapper<DmUser> lambdaWrapper = new MPJLambdaWrapper<>();
        lambdaWrapper.eq(DmUser::getPhone,user.getPhone());
        lambdaWrapper.selectAssociation(DmUser.class,DmUserDTO::getUser);
        return userMapper.selectJoinOne(DmUserDTO.class,lambdaWrapper);
    }

    @Override
    public boolean updateUser(DmUser user) {
        return userMapper.updateById(user) == 1;

    }
}

